package test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.management.ManagementFactory;

import com.sun.management.HotSpotDiagnosticMXBean;
import com.sun.management.VMOption;

public class ShowVmFlags {
	public static void main(String[] args) throws IOException {
		HotSpotDiagnosticMXBean diagnostics = newHotSpotDiagnosticMXBean();
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		try {
			String line;
			while ((line = reader.readLine()) != null) {
				line = trim(line);
				if (!line.isEmpty()) {
					try {
						VMOption option = diagnostics.getVMOption(line);
						System.out.printf("%s=%s # %s\n", option.getName(), option.getValue(), option.getOrigin());
					} catch (Throwable exc) {
						System.out.println("# " + exc.getMessage());
					}
				}
			}
		} finally {
			reader.close();
		}
	}

	static String trim(String line) {
		int idx = line.indexOf('#');
		if (idx != -1) line = line.substring(0, idx);
		return line.trim();
	}

	static HotSpotDiagnosticMXBean newHotSpotDiagnosticMXBean() throws IOException {
		return ManagementFactory.newPlatformMXBeanProxy(
				ManagementFactory.getPlatformMBeanServer(),
				"com.sun.management:type=HotSpotDiagnostic", HotSpotDiagnosticMXBean.class);
	}
}
